
package com.creditratingmodule.dao;
import com.creditratingmodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbCreditRatingDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbCreditRating tbCreditRating=null;
    private List<TbCreditRating> tbCreditRatingList=null;
    private Map<Integer,TbCreditRating> mapCreditRating=null;
    public boolean add(TbCreditRating inTbCreditRating){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_CreditRating(CreditRatingTypeId,CreditRatingScore) values(?,?)";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbCreditRating.getCreditRatingTypeId());
        pt.setString(2,inTbCreditRating.getCreditRatingScore());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inCreditRatingId){
        return UtilDAO.del("Tb_CreditRating", "CreditRatingId", inCreditRatingId);
    }
    public boolean edit(TbCreditRating inTbCreditRating){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_CreditRating set  CreditRatingTypeId=?,CreditRatingScore=? where CreditRatingId=?";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbCreditRating.getCreditRatingTypeId());
        pt.setString(2,inTbCreditRating.getCreditRatingScore());
        pt.setInt(3,inTbCreditRating.getCreditRatingId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbCreditRating getTbCreditRatingByCreditRatingId(int inCreditRatingId) {
        tbCreditRating=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CreditRating where CreditRatingId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inCreditRatingId);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCreditRating = getTbCreditRating(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbCreditRating;
    }
    public List<TbCreditRating> getTbCreditRatingAll() {
        tbCreditRatingList=new ArrayList<TbCreditRating>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CreditRating";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCreditRatingList.add(getTbCreditRating(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbCreditRatingList;
    }
    /**
     * 
     *@author qingyu zhang
     *@function:
     * @return
     *2011-5-13
     *下午04:09:51
     *ProjectManager
     *com.creditratingmodule.dao
     *Map<Integer,TbCreditRating>
     */
    public Map<Integer,TbCreditRating> getTbCreditRating(){
    	mapCreditRating = new HashMap<Integer,TbCreditRating>();
    	try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CreditRating";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
            	mapCreditRating.put(getTbCreditRating(rs).getCreditRatingId(), getTbCreditRating(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
            
        }
        return mapCreditRating;
    }
    public List<TbCreditRating> getTbCreditRatingAll(VskPage vp) {
        tbCreditRatingList=new ArrayList<TbCreditRating>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CreditRating";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_CreditRating", "CreditRatingId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCreditRatingList.add(getTbCreditRating(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbCreditRatingList;
    }
    public TbCreditRating getTbCreditRating(ResultSet rs) throws Exception{
        TbCreditRating tbCreditRating = new TbCreditRating();
        tbCreditRating.setCreditRatingId(rs.getInt("CreditRatingId"));
        tbCreditRating.setCreditRatingTypeId(rs.getInt("CreditRatingTypeId"));
        tbCreditRating.setCreditRatingScore(rs.getString("CreditRatingScore"));
        tbCreditRating.setCreateTime(rs.getString("CreateTime"));
        return tbCreditRating;
    }
}
